package com.lc1993929.mr.index;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * Created by LiuChang on 2017/11/6/006.
 */
public class IndexStepOne {

    public static class IndexStepOneMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        String fileName;

        //一个map程序运行实例在调我们的自定义mapper逻辑类时，首先会调用一次setup（）方法
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            FileSplit inputSplit = (FileSplit) context.getInputSplit();
            fileName = inputSplit.getPath().getName();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //    想生成   key：单词-文件名  value：1


            String line = value.toString();
            String[] words = line.split(" ");
            for (String word : words) {
                context.write(new Text(word + "-" + fileName), new IntWritable(1));
            }
        }

        //当一个map程序实例在处理完自己说负责的整个切片数据后，会调一次cleanup()方法
        @Override
        protected void cleanup(Context context) throws IOException, InterruptedException {

        }
    }

    public static class IndexStepOneReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int count = 0;
            for (IntWritable value : values) {
                count += value.get();
            }
            context.write(key, new IntWritable(count));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //要执行的jar文件
        job.setJarByClass(IndexStepOne.class);
        //指定mapper类和reducer类
        job.setMapperClass(IndexStepOneMapper.class);
        job.setReducerClass(IndexStepOneReducer.class);
        //指定mapper和reducer的输出的key和value的数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        //指定要解析的文件类型
        job.setInputFormatClass(TextInputFormat.class);
        //指定要解析的文件路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        //指定要输出的文件类型
        job.setOutputFormatClass(TextOutputFormat.class);
        //指定输出的文件路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        //设置分区计算规则
        job.setNumReduceTasks(Integer.parseInt(args[2]));

        // 向yarn提交job
        boolean res = job.waitForCompletion(true);

        System.exit(res ? 0 : 1);
    }
}
